package com.health.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.health.model.BloodPressure;

public class BloodPressureDAO extends HibernateDaoSupport
{
	private static final Log	log	= LogFactory.getLog(BloodPressureDAO.class);

	private SessionFactory		sessionFactory;

	private SimpleDateFormat	sdf	= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/***************************************************************************
	 * insert an BloodPressure Entity
	 **************************************************************************/
	public void insert(BloodPressure p)
	{
		HibernateTemplate tpl = new HibernateTemplate(getSessionFactory());
		tpl.save(p);
	}

	/***************************************************************************
	 * delete an BloodPressure Entity by Entity arg
	 **************************************************************************/
	public void delete(BloodPressure p)
	{
		HibernateTemplate tpl = new HibernateTemplate(getSessionFactory());
		tpl.delete(p);
	}

	/***************************************************************************
	 * delete an BloodPressure Entity by it's id
	 **************************************************************************/
	public void delete(String id)
	{
		BloodPressure p = new BloodPressure();
		p.setId(id);
		HibernateTemplate tpl = new HibernateTemplate(getSessionFactory());
		tpl.save(p);
	}

	/***************************************************************************
	 * update an BloodPressure Entity
	 **************************************************************************/
	public void update(BloodPressure p)
	{
		HibernateTemplate tpl = new HibernateTemplate(getSessionFactory());
		tpl.update(p);
	}

	/***************************************************************************
	 * get all datas which can match conditions without paging operation
	 **************************************************************************/
	public Collection<BloodPressure> selectListAll(BloodPressure p)
	{
		HibernateTemplate tpl = new HibernateTemplate(getSessionFactory());
		Collection<BloodPressure> collection = null;
		String sql = "select P from BloodPressure P where 1=1";
		if (null != p.getId() && !"".equals(p.getId().trim()))
		{
			sql += " and P.id = '" + p.getId() + "'";
		}
		else if (null != p.getUserId() && !"".equals(p.getUserId().trim()))
		{
			sql += " and P.userId = '" + p.getUserId() + "'";
		}
		else
		{
			return new ArrayList();
		}
		if (null != p.getExamTimeStart())
		{
			sql += " and P.examTime >= '" + sdf.format(p.getExamTimeStart())
					+ " 00:00:00" + "'";
		}
		if (null != p.getExamTimeEnd())
		{
			sql += " and P.examTime <= '" + sdf.format(p.getExamTimeEnd())
					+ " 23:59:59" + "'";
		}
		if (null != p.getLow1())
		{
			sql += " and P.low >= " + p.getLow1();
		}
		if (null != p.getLow2())
		{
			sql += " and P.low <= " + p.getLow2();
		}
		if (null != p.getHigh1())
		{
			sql += " and P.high >= " + p.getHigh1();
		}
		if (null != p.getHigh2())
		{
			sql += " and P.high <= " + p.getHigh2();
		}
		collection = tpl.find(sql);

		return collection;
	}

	/***************************************************************************
	 * get datas which can match conditions with paging
	 **************************************************************************/
	public Collection<BloodPressure> selectListPage(BloodPressure p)
	{
		int start = p.getStart();
		int limit = p.getLimit();
		Session session = null;
		HibernateTemplate tpl = new HibernateTemplate(getSessionFactory());
		Collection<BloodPressure> collection = null;
		String sql = "select P from BloodPressure P where 1=1";
		if (null != p.getUserId() && !"".equals(p.getUserId().trim()))
		{
			sql += " and P.userId = '" + p.getUserId() + "'";
		}
		else
		{
			return new ArrayList();
		}
		if (null != p.getExamTimeStart())
		{
			sql += " and P.examTime >= '" + sdf.format(p.getExamTimeStart())
					+ " 00:00:00" + "'";
		}
		if (null != p.getExamTimeEnd())
		{
			sql += " and P.examTime <= '" + sdf.format(p.getExamTimeEnd())
					+ " 23:59:59" + "'";
		}
		if (null != p.getLow1())
		{
			sql += " and P.low >= " + p.getLow1();
		}
		if (null != p.getLow2())
		{
			sql += " and P.low <= " + p.getLow2();
		}
		if (null != p.getHigh1())
		{
			sql += " and P.high >= " + p.getHigh1();
		}
		if (null != p.getHigh2())
		{
			sql += " and P.high <= " + p.getHigh2();
		}
		Query query = this.getSession().createQuery(sql);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		collection = query.list();
		return collection;
	}

	/***************************************************************************
	 * select by PK
	 **************************************************************************/
	public BloodPressure selectByPK(String id)
	{
		BloodPressure p = new BloodPressure();
		Session session = null;
		session = sessionFactory.openSession();
		session.beginTransaction();
		p = (BloodPressure) session.load(BloodPressure.class, id);
		return p;
	}

}
